package edu.umn.se.trap;

import java.util.ArrayList;
import java.util.Map;

import edu.umn.se.trap.exceptions.FormMemoryException;
/**
 * 
 * @author Kevin
 *
 */
public class FormMemory {
    /**
     * this represents the memory in which we are storing the forms
     */
    private ArrayList<TravelFormMetadata> formMemory;
    private User currentUser;
    
    public FormMemory(){
        formMemory = new ArrayList<TravelFormMetadata>();
        currentUser = null;
    }
    
    /**
     * 
     * @param user
     */
    public void setUser(User user) //throws newException
    {
        this.currentUser = user;
    }
    
    /**
     * 
     * @return
     */
    public User getUser()
    {
        return this.currentUser;
    }
    
    /**
     * 
     * @param user
     */
    public int getMemoryLength() //throws newException
    {
        return this.formMemory.size();
    }

    /**
     * 
     * @param newForm
     * @return
     * @throws FormMemoryException 
     */
    public int addForm(Map<String, String> newForm, String description) throws FormMemoryException
    {
        TravelFormMetadata form = new TravelFormMetadata();
        form.description = description;
        form.setFormMap(newForm);
        form.status = TravelFormProcessorIntf.FORM_STATUS.DRAFT;
        boolean wasAdded = this.formMemory.add(form);
        if(!wasAdded || !this.formMemory.contains(form))
            throw new FormMemoryException("Form couldn't be saved in memory");
        return this.formMemory.indexOf(form);
    }
    
    /**
     * 
     * @param form
     * @param formId
     * @throws FormMemoryException 
     */
    public void editForm(Map<String,String> form, int formId) throws FormMemoryException 
    {
        TravelFormMetadata oldForm = this.formMemory.get(formId);
        oldForm.setFormMap(form);
        this.formMemory.set(formId,oldForm);
        if(!this.formMemory.contains(oldForm))
            throw new FormMemoryException("Form couldn't be edited");
        
    }
    
    /**
     * 
     * @param formId
     * @return
     * @throws FormMemoryException 
     */
    public TravelFormMetadata getForm(int formId) throws FormMemoryException
    {
    	if(formId > this.formMemory.size())
    		throw new FormMemoryException("Form ID " + formId + " was outside the range of formIDs");
        TravelFormMetadata returner = this.formMemory.get(formId);
        if(returner == null)
            throw new FormMemoryException("Form data couldn't be found or returned");
        else return returner;
        
    }
    
    /**
     * 
     * @param formId
     */
    public void removeForm(int formId) //throws newException
    {
        this.formMemory.remove(formId);
    }
    
    /**
     * 
     */
    public void clearForms() throws FormMemoryException
    {
        this.formMemory.clear();
        if(!this.formMemory.isEmpty())
            throw new FormMemoryException("Form List wasn't properly cleared");
    }
}